package com.google.code.fluentjoiner.io.http;

import com.google.code.fluentjoiner.io.FileObject;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.io.IOUtils;

public class HttpFileObject implements FileObject
{
    private final HttpClient httpClient;
    private final String path;

    public HttpFileObject(final HttpClient httpClient, final String path)
    {
        this.httpClient = httpClient;
        this.path = path;
    }

    @Override
    public byte[] readContent()
    {
        InputStream inputStream = null;
        try
        {
            final GetMethod getMethod = new GetMethod(path);
            httpClient.executeMethod(getMethod);
            inputStream = getMethod.getResponseBodyAsStream();
            return IOUtils.toByteArray(inputStream);
        }
        catch (final IOException ioException)
        {
            throw new RuntimeException("IOException thrown while reading content.", ioException);
        }
        finally
        {
            IOUtils.closeQuietly(inputStream);
        }
    }

    @Override
    public void writeContent(final byte[] bytes)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}